{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from collections import defaultdict"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 导入原始数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_table('./data/me/three_week_data.txt',sep='\\t')\n",
    "test_data = pd.read_table('./data/orders_test_spu.txt',sep='\\t')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 通过前三周的数据构造训练集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>wm_order_id</th>\n",
       "      <th>wm_poi_id</th>\n",
       "      <th>aor_id</th>\n",
       "      <th>order_price_interval</th>\n",
       "      <th>order_timestamp</th>\n",
       "      <th>ord_period_name</th>\n",
       "      <th>order_scene_name</th>\n",
       "      <th>aoi_id</th>\n",
       "      <th>takedlvr_aoi_type_name</th>\n",
       "      <th>dt</th>\n",
       "      <th>wm_food_spu_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>178557</td>\n",
       "      <td>0</td>\n",
       "      <td>2334</td>\n",
       "      <td>6</td>\n",
       "      <td>&lt;29</td>\n",
       "      <td>1623061539</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>未知</td>\n",
       "      <td>20210607</td>\n",
       "      <td>39803</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>175118</td>\n",
       "      <td>1</td>\n",
       "      <td>3315</td>\n",
       "      <td>0</td>\n",
       "      <td>&lt;29</td>\n",
       "      <td>1623032193</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>20210607</td>\n",
       "      <td>61775</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>175118</td>\n",
       "      <td>1</td>\n",
       "      <td>3315</td>\n",
       "      <td>0</td>\n",
       "      <td>&lt;29</td>\n",
       "      <td>1623032193</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>20210607</td>\n",
       "      <td>49467</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>175118</td>\n",
       "      <td>1</td>\n",
       "      <td>3315</td>\n",
       "      <td>0</td>\n",
       "      <td>&lt;29</td>\n",
       "      <td>1623032193</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>20210607</td>\n",
       "      <td>15399</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>36208</td>\n",
       "      <td>2</td>\n",
       "      <td>2168</td>\n",
       "      <td>0</td>\n",
       "      <td>&lt;29</td>\n",
       "      <td>1623036350</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>20210607</td>\n",
       "      <td>21770</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id  wm_order_id  wm_poi_id  aor_id order_price_interval  \\\n",
       "0   178557            0       2334       6                  <29   \n",
       "1   175118            1       3315       0                  <29   \n",
       "2   175118            1       3315       0                  <29   \n",
       "3   175118            1       3315       0                  <29   \n",
       "4    36208            2       2168       0                  <29   \n",
       "\n",
       "   order_timestamp  ord_period_name order_scene_name  aoi_id  \\\n",
       "0       1623061539                3                0     NaN   \n",
       "1       1623032193                1                1     0.0   \n",
       "2       1623032193                1                1     0.0   \n",
       "3       1623032193                1                1     0.0   \n",
       "4       1623036350                1                0     1.0   \n",
       "\n",
       "  takedlvr_aoi_type_name        dt  wm_food_spu_id  \n",
       "0                     未知  20210607           39803  \n",
       "1                      0  20210607           61775  \n",
       "2                      0  20210607           49467  \n",
       "3                      0  20210607           15399  \n",
       "4                      0  20210607           21770  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 3381805 entries, 0 to 3381804\n",
      "Data columns (total 12 columns):\n",
      "user_id                   int64\n",
      "wm_order_id               int64\n",
      "wm_poi_id                 int64\n",
      "aor_id                    int64\n",
      "order_price_interval      object\n",
      "order_timestamp           int64\n",
      "ord_period_name           int64\n",
      "order_scene_name          object\n",
      "aoi_id                    float64\n",
      "takedlvr_aoi_type_name    object\n",
      "dt                        int64\n",
      "wm_food_spu_id            int64\n",
      "dtypes: float64(1), int64(8), object(3)\n",
      "memory usage: 309.6+ MB\n"
     ]
    }
   ],
   "source": [
    "data.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# spu训练数据生成\n",
    "* 下面两段代码，生成的每一行是一个用户id后面跟着他购买过的菜品（spu）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_dict = defaultdict(list)\n",
    "\n",
    "for tup in zip(data['user_id'],data['wm_food_spu_id']):\n",
    "    train_dict[tup[0]].append(tup[1])\n",
    "\n",
    "len(train_dict)\n",
    "\n",
    "with open('train.txt','w') as f:\n",
    "    for key in train_dict:\n",
    "        f.write(str(key))\n",
    "        for spu in train_dict[key]:\n",
    "            f.write(' ')\n",
    "            f.write(str(spu))\n",
    "        f.write('\\n')            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_dict = defaultdict(list)\n",
    "\n",
    "for tup in zip(test_data['user_id'],test_data['wm_order_id']):\n",
    "    test_dict[tup[0]].append(tup[1])\n",
    "\n",
    "len(test_dict)\n",
    "\n",
    "with open('test.txt','w') as f:\n",
    "    for key in test_dict:\n",
    "        f.write(str(key))\n",
    "        f.write(' ')\n",
    "        f.write('0')\n",
    "        f.write('\\n')            "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过上面可以得到，测试集中有用户108738个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**这个程序输出train.txt,test.txt,分别是后续GNN模型的训练和测试文件**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
